<?php

class stYoutubeToolboxCronjob {

    public $config = array();
    public $addOn = array();

    public function __construct($config) {
        $this->config = $config;
        if (empty($this->config ['pluginDir']))
            $this->config ['pluginDir'] = $config['fileDir'].DIRECTORY_SEPARATOR;

        $this->loadLib();
        $this->excute();
    }

    public function loadLib() {
        $fileDir = $this->config ['fileDir'];
        $version = $this->config ['plugin_version'];
        require_once $fileDir . '/libs/' . $version . '/classes/stcontroller.php';
        require_once $fileDir . '/libs/' . $version . '/classes/stmodel.php';
        require_once $fileDir . '/libs/' . $version . '/classes/stview.php';
        require_once $fileDir . '/libs/' . $version . '/classes/stfunctions.php';
        require_once $fileDir . '/libs/' . $version . '/functions/core.php';
        require_once $fileDir . '/functions.php';
        require_once $fileDir . '/controller.php';
    }

    public function excute() {
        $config = $this->config;
        $controllerName = $config['plugin_name'] . "Controller";
        $controllerObject = new $controllerName($config);
        require_once $controllerObject->config['pluginDir'] . 'stYoutubeToolboxclass.php';
        $modelDir =  dirname(__FILE__);
        $modelDir = explode(DIRECTORY_SEPARATOR.'Toolboxes', $modelDir);
        $modelDir = $modelDir[0].DIRECTORY_SEPARATOR;
        $st_cronjob_log_model = $controllerObject->loadmodel("st_cronjob_log", $modelDir);
        $file_name = __FILE__;
        $data = array(
            'id'            => NULL,
            'cronjob_name'  => 'cronjob Statistic',
            'toolbox_name'  => $config['plugin_name'],
            'url'           => $file_name,
            'status'        => 0,
            'ctime'         => date_i18n('Y-m-d H:i:s')
        );
        $st_cronjob_log_model->saveCronLog($data);
        $log_id = $st_cronjob_log_model-> getID($data['cronjob_name'], $data['ctime']);

        $stYoutubeToolbox = new stYoutubeToolboxclass($controllerObject->config, $controllerObject->option);
        try{
            $stYoutubeToolbox->cronjobStatistic();
            $data = array(
                'id'            => $log_id,
                'status'        => 1
            );
            $st_cronjob_log_model->saveCronLog($data);
        }catch (Exception $e){
            $controllerObject->functions->cronjobMessage($data);
        }
//        $stYoutubeToolbox->cronjobGetComment();
//        die;
    }

}

$fileDir =  dirname(__FILE__);
$dir_file1 = explode(DIRECTORY_SEPARATOR.'wp-content'.DIRECTORY_SEPARATOR, $fileDir);
define('ABSPATH_FB', $dir_file1[0] . '/');
require_once( ABSPATH_FB . 'wp-load.php' );

$fileDir =  dirname(__FILE__);
$fileDir = str_replace(DIRECTORY_SEPARATOR.'cronjob', '', $fileDir);
$config = include $fileDir . DIRECTORY_SEPARATOR.'config.php';
$config['fileDir'] = $fileDir;
$pluginClass = new stYoutubeToolboxCronjob($config);
?>